#include <Def.h>
#include "AddonPackagePrivate.h"

#define PROCESS_EVENT \
auto itr = addonList.begin();\
while(itr != addonList.end())\
{\
    auto addon = *itr;\
	if(addon && addon->process(event))\
		return true;\
	itr++;\
}\
return false;

AddonPackagePrivate::AddonPackagePrivate()
{
}

AddonPackagePrivate::AddonPackagePrivate(const ModuleInfo& info):
moduleInfo(info)
{
}

void AddonPackagePrivate::setPackageInfo(const ModuleInfo& info)
{
	moduleInfo = info;
}

void AddonPackagePrivate::initial()
{
    
}

void AddonPackagePrivate::destroy()
{
	foreach(auto addon,addonList)
		addon->destroy();
	addonList.clear();
	delete this;
}

bool AddonPackagePrivate::process(const QMouseEvent& event)
{
	PROCESS_EVENT
}

bool AddonPackagePrivate::process(const QKeyEvent& event)
{
	PROCESS_EVENT
}

bool AddonPackagePrivate::process(const QWheelEvent& event)
{
	PROCESS_EVENT
}

/*
void AddonPackagePrivate::receivedMessage(const AddonMessage& message)
{
	if(message.toPackage == moduleInfo.name || message.toPackage.isEmpty())
	{
		foreach(auto addon,addonList)
		{
			if(addon)
				addon->receivedMessage(message);
		}
	}
}

void AddonPackagePrivate::sendMessage(const AddonMessage& message)
{
	if(message.toPackage == moduleInfo.name || message.toPackage.isEmpty())
	{
		foreach(auto addon,addonList)
		{
			if(addon)
				addon->sendMessage(message);
		}
	}
}*/

ModuleInfo AddonPackagePrivate::getAddonInfo()const
{
	return moduleInfo;
}
	
void AddonPackagePrivate::registerAddon(Addon* addon)
{
	if(addon)
	{
		addonNameList.push_back(addon->getAddonInfo().name);
		addonList.push_back(addon);
	}
}

QStringList AddonPackagePrivate::getAddonNameList()const
{
	return addonNameList;
}

Addon* AddonPackagePrivate::getAddonByName(const QString& name)const
{
	if(!addonNameList.contains(name))
		return nullptr;

	foreach(auto item,addonList)
	{
		if(item->getAddonInfo().name == name)
			return item;
	}
	return nullptr;
}

#undef PROCESS_EVENT